﻿using System;
using System.Management;
using System.Security.Cryptography;
using System.Text;

namespace GSMS.Common
{
    public class Utils
    {
        public static string GetPhysicalId()
        {
            byte[] result;
            try
            {
                result = (new SHA1CryptoServiceProvider()).ComputeHash(Encoding.UTF8.GetBytes(GetMacAddress()));
            }
            catch (Exception)
            {
                try
                {
                    result = (new SHA1CryptoServiceProvider()).ComputeHash(Encoding.UTF8.GetBytes(GetVolumeSerial("")));
                }
                catch (Exception)
                {
                    result = (new SHA1CryptoServiceProvider()).ComputeHash(Encoding.UTF8.GetBytes(GetCpuId()));
                }
            }
            return Convert.ToBase64String(result);
        }

        private static string GetCpuId()
        {
            string cpuInfo = String.Empty;
            string temp = String.Empty;
            var mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (cpuInfo == String.Empty && mo.Properties["ProcessorId"].Value != null)
                {
                    // only return cpuInfo from first CPU
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
            }
            return cpuInfo;
        }

        private static string GetMacAddress()
        {
            var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            string MACAddress = String.Empty;
            foreach (ManagementObject mo in moc)
            {
                if (MACAddress == String.Empty) // only return MAC Address from first card
                {
                    if ((bool)mo["IPEnabled"])
                    {
                        MACAddress = mo["MacAddress"].ToString();
                    }
                }
                mo.Dispose();
            }
            MACAddress = MACAddress.Replace(":", "");
            return MACAddress;
        }

        private static string GetAllMacAddresses()
        {
            string ret = "";
            var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            int count = 0;
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"])
                {
                    ret += mo["MacAddress"].ToString() + "|";
                }
                mo.Dispose();
                count++;
                if (count == 2)
                {
                    break;
                }
            }
            ret = ret.Replace(":", "");
            return ret;
        }

        private static string GetVolumeSerial(string strDriveLetter)
        {
            if (strDriveLetter == "" || strDriveLetter == null)
            {
                strDriveLetter = "C";
            }
            var disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + strDriveLetter + ":\"");
            disk.Get();
            return disk["VolumeSerialNumber"].ToString();
        }
    }
}
